package com.example.ruralservicedigitalplatform.service.impl;

import com.example.ruralservicedigitalplatform.context.BaseContext;
import com.example.ruralservicedigitalplatform.domain.dto.ShoppingCartDto;
import com.example.ruralservicedigitalplatform.domain.po.Item;
import com.example.ruralservicedigitalplatform.domain.po.ShoppingCart;
import com.example.ruralservicedigitalplatform.domain.po.Summary;
import com.example.ruralservicedigitalplatform.mapper.ItemMapper;
import com.example.ruralservicedigitalplatform.mapper.ShoppingCartMapper;
import com.example.ruralservicedigitalplatform.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private ItemMapper itemMapper;

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long villagerId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .quantity(1)
                .villagerId(villagerId)
                .build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 添加购物车
     *
     * @param shoppingCartDto
     */
    @Override
    public void addShoppingCart(ShoppingCartDto shoppingCartDto) {
        //判断当前加入购物车中的商品是否已经存在了
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDto, shoppingCart);
        Long villagerId = BaseContext.getCurrentId();
        shoppingCart.setVillagerId(villagerId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在，只需数量加一
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setQuantity(cart.getQuantity() + 1);

            cart.setAmount(cart.getAmount().add(cart.getPrice()));
            shoppingCartMapper.updateQuantityById(cart);
            //更新总价
            shoppingCartMapper.updateTotal(villagerId);

        } else {
            //如果不存在，需插入一条购物车数据
            Long itemId = shoppingCart.getItemId();

            Item item = itemMapper.getById(itemId);
            shoppingCart.setName(item.getName());
            shoppingCart.setImage(item.getImage());
            shoppingCart.setAmount(item.getPrice());
            shoppingCart.setQuantity(1);
            shoppingCart.setPrice(item.getPrice());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
            //根据villagerId查寻总价表
            Summary sy = shoppingCartMapper.selectByVillagerId(villagerId);
            if(sy == null){
                //为空就插入新的总价
                Summary summary =  Summary.builder()
                        .total(item.getPrice())
                        .villagerId(villagerId)
                        .build();
                shoppingCartMapper.insertTotal(summary);
            }else {
                //不为空则更新总价
                shoppingCartMapper.updateTotal(villagerId);
            }
        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long villagerId =BaseContext.getCurrentId();
        //清空购物车
        shoppingCartMapper.deleteByVillagerId(villagerId);
        //清空购物车后，清空总价
        shoppingCartMapper.deleteTotalByVillagerId(villagerId);
    }

    /**
     * 删除购物车的一个商品
     * @param shoppingCartDto
     */
    @Override
    public void deleteOne(ShoppingCartDto shoppingCartDto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDto,shoppingCart);

        shoppingCart.setVillagerId(BaseContext.getCurrentId());
        //查询购物车中有没有商品
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if(list !=null && list.size()>0){
            shoppingCart = list.get(0);
            //判断商品数量是否为1
            Integer quantity = shoppingCart.getQuantity();
            if(quantity == 1){
                //删除购物车的一个商品
                shoppingCartMapper.deleteById(shoppingCart.getShoppingId());
                //更新总价
                shoppingCartMapper.updateTotal(BaseContext.getCurrentId());

            }else {
                //判断商品数量不为1，商品数量减一
                shoppingCart.setQuantity(shoppingCart.getQuantity()-1);
                shoppingCart.setAmount(shoppingCart.getAmount().subtract(shoppingCart.getPrice()));
                //更新购物车
                shoppingCartMapper.updateQuantityById(shoppingCart);
                //更新总价
                shoppingCartMapper.updateTotal(BaseContext.getCurrentId());
            }

        }
    }
}

